package com.wuhan.template.module.home.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;

import java.lang.reflect.InvocationTargetException;

import butterknife.BindView;

public class WebVideoActivity extends BaseActivity {

    @BindView(R.id.jiaoyu_webView)
    protected WebView webView;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_web_video;
    }

    @Override
    protected void initViews() {
        String video = getIntent().getStringExtra("video");
        WebSettings setting = webView.getSettings();
        setting.setJavaScriptEnabled(true);
        setting.setDomStorageEnabled(true);
        setting.setDatabaseEnabled(true);
        setting.setPluginState(WebSettings.PluginState.ON);
        setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        setting.setDefaultTextEncodingName("UTF-8");

        setting.setJavaScriptCanOpenWindowsAutomatically(true);
        setting.setAllowFileAccess(true);
        setting.setLoadWithOverviewMode(true);
        setting.setUseWideViewPort(true);
        webView.setVisibility(View.VISIBLE);


        webView.setWebChromeClient(new WebChromeClient());
       // webView.setWebViewClient(new MyWebViewClient());
        try {
            webView.getClass().getMethod("onPause").invoke(webView,(Object[])null);
            webView.getClass().getMethod("onResume").invoke(webView,(Object[])null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        webView.loadUrl(video);
    }


    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.endsWith(".mp4")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse(url), "video/*");
                view.getContext().startActivity(intent);

                return true;
            } else {
                return super.shouldOverrideUrlLoading(view, url);
            }
        }
    }
}
